package com.ljxt.dt.service.storemanagementservice.impl;

import com.ljxt.dt.dao.storemanagementdao.DetailDao;
import com.ljxt.dt.dao.storemanagementdao.StoreDao;
import com.ljxt.dt.dao.storemanagementdao.impl.DetailDaoImpl;
import com.ljxt.dt.dao.storemanagementdao.impl.StoreDaoImpl;
import com.ljxt.dt.domain.HandlerInfo;
import com.ljxt.dt.domain.ReceptDetail;
import com.ljxt.dt.domain.ReceptInfo;
import com.ljxt.dt.service.storemanagementservice.DetailService;
import com.ljxt.dt.util.ServiceUtil;
import com.ljxt.dt.vo.storemanagement.HandlerInfoVO;
import com.ljxt.dt.vo.storemanagement.ReceptDetailVO;
import com.ljxt.dt.vo.storemanagement.ReceptVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-11 21:41
 * @copyright 老九学堂
 */

public class DetailServiceImpl implements DetailService {
    private DetailDao admin = new DetailDaoImpl();
    private StoreDao storeDaoAdmin = new StoreDaoImpl();
    @Override
    public ReceptVO selectDetail(String id) {
        ReceptInfo receptInfo = admin.selectOnly(id);
        ReceptVO receptVO = new ReceptVO();
        //将值复制进去
        receptVO.setRemakers(receptInfo.getRemakers());
        receptVO.setReceptNo(receptInfo.getReceptNo());
        receptVO.setOrderNo(receptInfo.getOrderNo());
        receptVO.setCreatedate(ServiceUtil.dateFormat(receptInfo.getCreatedate()));
        receptVO.setId(receptInfo.getId());

        String result = storeDaoAdmin.selectResultString(receptInfo.getStateId());
        String name = storeDaoAdmin.selectName(receptInfo.getTypeId());
        receptVO.setResult(result);
        receptVO.setName(name);

        return receptVO;
    }

    @Override
    public List<ReceptDetailVO> selectDetailGoods(String id) {

        List<ReceptDetail>  list = admin.selectDetailGoods(id);
        List<ReceptDetailVO> receptVOList = new ArrayList<>();

        for (ReceptDetail r:list) {
            ReceptDetailVO receptDetailVO = new ReceptDetailVO();
            receptDetailVO = (ReceptDetailVO) ServiceUtil.setObj(r,receptDetailVO);
            //添加货架信息和商品属性
            //获取商品属性
            String[] strings = r.getAttrId().split(",");
           Object[] params = new Object[strings.length];


            for (int i=0 ;i<strings.length;i++){

                params[i] = (strings[i]);
            }
            //获取属性

            List<String> attrs = admin.selectDetailAttrs(params);

            if(attrs.size() == 0){
                receptDetailVO.setName("");
                receptDetailVO.setValue("");
            }else if (attrs.size() == 1) {

                receptDetailVO.setName(attrs.get(0));
                receptDetailVO.setValue("");

            }else{
                receptDetailVO.setName(attrs.get(0));

                receptDetailVO.setValue(attrs.get(1));
            }

           receptDetailVO.setShelves(admin.selectShelevs(r.getShelvesId()));
           //获取供应商
            String gys = admin.selectGys(r.getGoodsId());
            receptDetailVO.setGys(gys);

            receptVOList.add(receptDetailVO);
        }
        return receptVOList;
    }

    @Override
    public List<HandlerInfoVO> selectInfo(String id) {

        List<HandlerInfo> list = admin.selectInfo(id);
        //判断如果list为null则new一个
        if(list == null){
            list = new ArrayList<>();
        }
        List<HandlerInfoVO> handlerInfoVOS = new ArrayList<>();

        for (HandlerInfo h:list) {
            HandlerInfoVO handlerInfoVO = new HandlerInfoVO();
            handlerInfoVO = (HandlerInfoVO) ServiceUtil.setObj(h,handlerInfoVO);
            handlerInfoVO.setType(storeDaoAdmin.selectInfoValue(h.gethandlerTypeId()));
            handlerInfoVO.setResult(storeDaoAdmin.selectInfoResult(h.getResultId()));
            //处理时间

            handlerInfoVO.setInfoTime(ServiceUtil.dateFormat(h.getTime()));

            handlerInfoVOS.add(handlerInfoVO);
        }

        return handlerInfoVOS;
    }

    @Override
    public int addGoods(ArrayList<ReceptDetailVO> receptDetailVOS) {

        //先将入库单的状态改变和经办信息添加
      int rows  =   storeDaoAdmin.submitInfo(receptDetailVOS.get(0).getReceptNo());

      //添加经办信息
       rows+=storeDaoAdmin.insertBill(receptDetailVOS.get(0).getReceptNo(),"王五",3,4);

        //循环将这些商品信息添加对应的入库信息
        for (ReceptDetailVO receptDetailVO : receptDetailVOS) {

            storeDaoAdmin.insertStock(receptDetailVO);
            //更新对订单对应的信息
            storeDaoAdmin.updateBill(receptDetailVO);
            //添加库存信息
            storeDaoAdmin.insertStockGoodsInfo(receptDetailVO);
        }
        return rows;
    }
}
